package dk.iha;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;

public class AccelerometerSensor implements SensorEventListener, ISensor {

	SensorManager sensorManager;
	float x = 0;
	float y = 0;
	float z = 0;
	double calibration = SensorManager.STANDARD_GRAVITY;
	List<Float> data_x = null; 
	List<Float> data_y = null; 
	List<Float> data_z = null; 
	Handler accelerometerHandler = null;
	Boolean isRunning = false;

	
	public AccelerometerSensor(Context ctx) {
		sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
	}
	
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

	public void onSensorChanged(SensorEvent event) {
		x = event.values[0];
		y = event.values[1];
		z = event.values[2];
	}
	
	//Start logging for a given period of time (in ms)
	public void start(){
		//SharedData.getInstance().addDebugDataLog_t("AccelerometerSensor.startLogging");
		isRunning = true;
		//initialize storage
		data_x = new ArrayList<Float>();
		data_y = new ArrayList<Float>();
		data_z = new ArrayList<Float>();

		//initialisr variables to 0. 0,0,0 in result is an uninitialised measurement
		x = 0;
		y = 0;
		z = 0;

		//Register accelerometer listener
		Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		sensorManager.registerListener(this, accelerometer,	SensorManager.SENSOR_DELAY_FASTEST);
		
		//Runs every 100 ms as defined in ACCELEROMETER_SAMPLE_PERIOD
		accelerometerHandler = new Handler();
		//run the datacollection on start
		AccelerometerDataCollectionTimeout.run();
	}
	
    private Runnable AccelerometerDataCollectionTimeout = new Runnable() {
    	public void run() {
    		//Set up the runnable to run in  ACCELEROMETER_SAMPLE_PERIOD  (ms) again
    		accelerometerHandler.postDelayed(AccelerometerDataCollectionTimeout, SharedPreferences.ACCELEROMETER_SAMPLE_PERIOD);
    		//do the actual data collection to a data structure
//			data.add(currentAcceleration);
    		//uninitialised measurement check. discard this measurement
    		if(x == 0 && y == 0 && z == 0)
    			return;
			data_x.add(x);
			data_y.add(y);
			data_z.add(z);
    	}
    };

	
	public void stop(){
		isRunning = false;
		//Cancel timer as measurementPeriod has elapsed
		accelerometerHandler.removeCallbacks(AccelerometerDataCollectionTimeout);
		//Stop listening for accelerometer measurements
		AccelerometerSensor.this.sensorManager.unregisterListener(AccelerometerSensor.this);
		//SharedData.getInstance().addDebugDataLog_t("AccelerometerSensor.stopLogging");
	}
	/**
	 * @return accelerometer x axis data
	 */
	public List<Float> getData_x(){
		return data_x;
	}
	/**
	 * @return accelerometer y axis data
	 */
	public List<Float> getData_y(){
		return data_y;
	}
	/**
	 * @return accelerometer z axis data
	 */
	public List<Float> getData_z(){
		return data_z;
	}

	public boolean isRunning() {
		return isRunning;
	}
}
